package Binary_Search_Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-02-07
 * Time: 14:37
 */
public class BST {
    static class TreeNode {
        private int val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    private TreeNode root;

    public boolean Find(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                return true;
            }
        }

        return false;
    }

    public boolean Insert(int key) {
        // 判断是否是第一次插入
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                // 不能插入一样的值
                return false;
            }
        }
        TreeNode node = new TreeNode(key);
        // cur == null
        if (parent.val > key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        return true;
    }

    public boolean Remove(int key) {
        // 先找节点
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                RemoveNode(parent, cur);
                return true;
            }
        }
        return false;
    }

    private void RemoveNode(TreeNode parent, TreeNode cur) {
        // 此时判断 cur 的左右子树是否为空进行判断
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (parent.right == cur) {
                parent.right = cur.right;
            } else {
                parent.left = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (parent.right == cur) {
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }
        } else {
            // 此时都不为空 需要找替换法  左边的最小值  右边的最大值
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;// 进行交换
            if (target == targetParent.left) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }

}
